var iframe_load = {
    el:"",  //iframe挂载点
    sum_href:['index_main','tables','calendar','form',['table-list','table-list-img']],
    sidebar_tag:['index_main','tables','calendar','form','table-list','table-list-img'],  //侧边栏tag
    init:function(options){
        this.el = options.el;
        this.iframe_install();
        this.bind_href(this.sum_href,$("ul.sidebar-nav"));
    },
    //设置iframe框架相关属性
    iframe_install:function(){
        var height_i = $(window).height() - $("header").height() - 32;
        $("#index_main").height(height_i);
    },
    //绑定侧边栏点击事件
    bind_href:function(sum_href,tag){
        var that = this;
        for(var i = 0; i < sum_href.length; i++){
            let item = sum_href[i];  //此处只能用let进行块级作用域声明
            var type = typeof item;
            if(type === "string"){  //如果对象是字符串
                let src = item + ".html";
                tag.children(".sidebar-nav-link").eq(i).unbind().on("click",function(){
                    //改变选中状态
                    $("ul.sidebar-nav").find("a.active").removeClass("active");  //此处必须用$("ul.sidebar-nav")进行选中
                    $(this).find("a").addClass("active");
                    if(item.length == 0){
                        console.log("Incoming arguments are null");
                    }else{
                        //判断当前窗口是否处于active
                        var content = $(this).find("a").text();
                        that.judge_exist(item,content);
                    }
                })
            }else if(type === "object"){  //如果不是字符串，则是数组
                //递归调用
                that.bind_href(item,tag.children(".sidebar-nav-link").eq(i).children("ul.sidebar-nav"));
            }
        }
    },
    //判断当前点击是否存在于导航栏
    judge_exist:function(item,content){
        var that = this;
        var exist_tag = [];
        for(var i = 0; i < $(".daohang").find("li").length; i++){
            exist_tag.push($(".daohang").find("li").eq(i).attr("tag"));
        }
        var now_src = $(".daohang").find("li.active").attr("tag");
        if(item == now_src){ //如果点击对象正在显示
            return;
        }
        if($.inArray(item,exist_tag) == -1){  //如果点击对象不在导航栏队列里
            that.add_daohang(item,content);   
        }else{
            $(".daohang").find("li.active").removeClass("active");
            $("[tag='"+item+"']").addClass("active");
            $(that.el).attr("src",item+".html");
            that.tab_animate();
        }
    },
    //新增导航
    add_daohang:function(item,content){
        var that = this;
        var dom = document.createElement("li"),
            dom_btn = document.createElement("button"),
            dom_a = document.createElement("a");
        $(dom).attr("class","active").attr("tag",item);
        $(dom_btn).attr("type","button").attr("class","am-btn am-btn-default am-radius am-btn-xs").text(content);
        $(dom_a).attr("class","am-close am-close-spin").attr("data-am-modal-close","").text("x");
        $(dom_btn).append(dom_a);
        $(dom).append(dom_btn);
        
        $(".daohang").find("li.active").removeClass("active");
        $(".daohang").find("ul").append(dom);
        $(that.el).attr("src",item+".html");

        //添加事件
        that.daohang_operation();
        that.tab_animate();
    },
    //导航操作事件
    daohang_operation:function(){
        var that = this;
        //导航切换事件
        $(".daohang").find("li").unbind().on("click",function(){
            $(".daohang").find("li.active").removeClass("active");
            $(this).addClass("active");
            $(that.el).attr("src",$(this).attr("tag")+".html");
            that.tab_animate();

            //对应侧边栏选中事件
            var tag = $(this).attr("tag");
            if($.inArray(tag,that.sidebar_tag) !== -1){  //如果点击tag存在于sidebar_tag中
                $("ul.sidebar-nav").find("a").removeClass("active");
                $("[s-tag='"+tag+"']").children("a").addClass("active");
            }
        });

        //导航关闭按钮
        $(".daohang").find("li").find("a").unbind().on("click",function(){
            var src = $(this).parents("li").attr("tag");
            var next_src = $(this).parents("li").prev().attr("tag") + ".html";
            var next_tag = $(this).parents("li").prev();

            if($(".daohang").find(".active").attr("tag") !== src){  //如果当前关闭的是不active对象
                $(this).parents("li").remove();
            }else{
                $(this).parents("li").remove();
                $(that.el).attr("src",next_src);
                $(next_tag).addClass("active");
                that.tab_animate();
            }
        });
    },
    //动画加载
    tab_animate:function(){
        var that = this;
        layer.load();
        var left_ = $(".left-sidebar").is(".active") ? -5 : $(".left-sidebar").width() / 2 - 5;
        $(".layui-layer-content").css({
            "margin-top":$("#index_main").css("margin-top")/2,
            "margin-left":left_,
        });
        $(that.el).css("opacity",0);
        $(that.el).load(function(){
            setTimeout(function(){
                layer.closeAll();
                $(that.el).stop().animate({"opacity":1});
            }, 300)
        });
    },
}